import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wan_android/bean/article_list_bean.dart';
import 'package:flutter_wan_android/bean/wechat_bean.dart';
import 'package:flutter_wan_android/blocs/article_bloc.dart';
import 'package:flutter_wan_android/blocs/base/bloc_provider.dart';
import 'package:flutter_wan_android/blocs/wechat_bloc.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/dimens.dart';
import 'package:flutter_wan_android/res/strings.dart';
import 'package:flutter_wan_android/utils/navigator_utils.dart';
import 'package:flutter_wan_android/widgets/app_bar_widget.dart';
import 'package:flutter_wan_android/widgets/empty_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'article/article_page.dart';

class WeChatPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _WeChatPageState();
  }
}

class _WeChatPageState extends State<WeChatPage> {
  WeChatBloc _bloc;
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    _bloc = BlocProvider.of<WeChatBloc>(context);
    _initData();
  }

  void _initData() {
     _bloc.getWeChatNum();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar(
        '公众号',
        centerTitle: true,
      ).build(context),
      body: StreamBuilder<List<WeChatBean>>(
          stream: _bloc.stream,
          builder: (context, AsyncSnapshot<List<WeChatBean>> snapshot) {
            _refreshController.refreshCompleted();


            if (snapshot.hasError) {
              return ErrorView(error: snapshot.error.toString(),onTap: (){
                  _initData();
              },);
            }

            if (!snapshot.hasData) {
              return LoadingView();
            }



            return SmartRefresher(
                controller: _refreshController,
                onRefresh: () async {
                  _initData();
                },
                header: WaterDropMaterialHeader(
                  backgroundColor: XColors.refreshStyleColor,
                ),
                enablePullDown: true,
                enablePullUp: false,
                child: GridView.builder(
                  padding: EdgeInsets.all(10),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3,
                      childAspectRatio: 1.0,
                      mainAxisSpacing: 10,
                      crossAxisSpacing: 10),
                  itemBuilder: (context, int index) {
                    return InkWell(
                      onLongPress: (){

                      },
                      onTap: (){
                        WeChatBean item=snapshot.data.elementAt(index);
                        NavigatorUtils.push(context, BlocProvider<ArticleBloc>(bloc: ArticleBloc(),
                            child: ArticlePage(title: item.name,chapterId: item.id,)));
                      },
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(10.0),
                        child: Container(
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            color: XColors.randomColor(),
                          ),
                          child: Text(
                            snapshot.data.elementAt(index).name,
                            style: TextStyle(color: XColors.white, shadows: [
                              Shadow(
                                  color: XColors.primaryColor,
                                  offset: Offset(4, 8),
                                  blurRadius: 5),
                            ]),
                          ),
                        ),
                      ),
                    );
                  },
                  itemCount: snapshot.data != null ? snapshot.data.length : 0,
                ));
          }),
    );
  }
}
